const parse = (message) => {
  // 根据消息类型进行不同解析
  switch(message.msg_type) {
    case 1: // 文本消息
      return parseTextMsg();
    case 3: // 图片消息
      return parseImageMsg();
    case 49: // 引用消息
      return parseQuoteMsg();
    default:
      return {
        id: message.msg_id,
        senderId: message.from_user_name.str,
        senderName: message.from_user_name.str,
        senderAvatar: '',
        actualSenderId: '',
        content: message.content?.str || '',
        recipientId: message.to_user_name.str,
        timestamp: new Date(message.create_time * 1000).toISOString(),
        isGroupMessage: message.from_user_name.str.includes('@chatroom'),
        msgType: message.msg_type || 1
      };
  }

  function parseTextMsg() {
    const textMsg =  {
      id: message.msg_id,
      senderId: message.from_user_name.str,
      senderName: message.from_user_name.str,
      senderAvatar: '',
      actualSenderId: '',
      lastMessage:'',
      content: message.content.str,
      recipientId: message.to_user_name.str,
      timestamp: new Date(message.create_time * 1000).toISOString(),
      isGroupMessage: message.from_user_name.str.includes('@chatroom'),
      msgType: message.msg_type || 1,
      tag: 'received'
    };

     // 如果是群消息，拆分content获取实际发言人和内容
     if (textMsg.isGroupMessage) {
      const contentParts = textMsg.content.split(':\n');
      if (contentParts.length === 2) {
        textMsg.actualSenderId = contentParts[0];
        textMsg.content = contentParts[1];
      }
    };
    return textMsg;
  }

  function parseImageMsg() {
    const imageMessage = {
      id: message.msg_id,
      senderId: message.from_user_name.str,
      senderName: message.from_user_name.str,
      senderAvatar: '',
      actualSenderId: '',
      // 使用imageBuffer作为图片消息的content，如果不存在则使用空字符串
      content: `${message.imageBuffer}`,
      recipientId: message.to_user_name.str,
      timestamp: new Date(message.create_time * 1000).toISOString(),
      isGroupMessage: message.from_user_name.str.includes('@chatroom'),
      msgType: message.msg_type,
      tag: 'received'
    };
    if (imageMessage.isGroupMessage) {
      const contentParts = message.content.str.split(':\n');
      if (contentParts.length === 2) {
        imageMessage.actualSenderId = contentParts[0];
      }
    }
    return imageMessage;
  }

  function parseQuoteMsg() {
    try {
      // 解析XML内容
      const xmlContent = message.content.str;
      
      // 提取引用消息的关键信息
      const titleMatch = /<title>([^<]+)<\/title>/.exec(xmlContent);
      const referContentMatch = /<content>([^<]+)<\/content>/.exec(xmlContent);
      const displayNameMatch = /<displayname>([^<]+)<\/displayname>/.exec(xmlContent);
      const fromUserMatch = /<fromusr>([^<]+)<\/fromusr>/.exec(xmlContent);
      const createTimeMatch = /<createtime>([^<]+)<\/createtime>/.exec(xmlContent);
      
      // 构建引用消息对象
      const quoteMsg = {
        id: message.msg_id,
        senderId: message.from_user_name.str,
        senderName: message.from_user_name.str,
        senderAvatar: '',
        actualSenderId: '',
        content: titleMatch ? titleMatch[1] : '',
        recipientId: message.to_user_name.str,
        timestamp: new Date(message.create_time * 1000).toISOString(),
        isGroupMessage: message.from_user_name.str.includes('@chatroom'),
        msgType: message.msg_type,
        tag: 'received',
        // 引用消息特有字段
        quoteInfo: {
          referContent: referContentMatch ? referContentMatch[1] : '',
          referSenderName: displayNameMatch ? displayNameMatch[1] : '',
          referSenderId: fromUserMatch ? fromUserMatch[1] : '',
          referTimestamp: createTimeMatch ? new Date(parseInt(createTimeMatch[1]) * 1000).toISOString() : ''
        }
      };
      
      // 如果是群消息，处理实际发送者
      if (quoteMsg.isGroupMessage) {
        // 从fromusername标签中获取实际发送者
        const fromUserNameMatch = /<fromusername>([^<]+)<\/fromusername>/.exec(xmlContent);
        if (fromUserNameMatch && fromUserNameMatch[1]) {
          quoteMsg.actualSenderId = fromUserNameMatch[1];
        }
      }
      
      return quoteMsg;
    } catch (error) {
      console.error('解析引用消息失败:', error);
      // 解析失败时返回基本消息格式
      return {
        id: message.msg_id,
        senderId: message.from_user_name.str,
        senderName: message.from_user_name.str,
        senderAvatar: '',
        actualSenderId: '',
        content: '引用消息解析失败',
        recipientId: message.to_user_name.str,
        timestamp: new Date(message.create_time * 1000).toISOString(),
        isGroupMessage: message.from_user_name.str.includes('@chatroom'),
        msgType: message.msg_type,
        tag: 'received'
      };
    }
  }
};

export { parse };